<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ################


class JFX_Module_Vms extends JFX_Module
{
    public $keyname = 'vms';
    public $title = 'Version Management Server';
    public $version = '0.1.0';
    protected $VMS_Config = array();
    public $dependencies = array(
        array(
            'keyname' =>'iusers',
            'version' => '0.1.0'

            )
        );


    public function __construct(){
        parent::__construct();
        // automatically load our configuration settings once only
        if(count($this->VMS_Config) == 0) $this->LoadConfig();
        if(count($this->VMS_Config) == 0){
            // probably not installed, can't proceed
            return;
        };
        
        $DB = JFX::registry('db');
        $CONFIG = JFX::registry('config');

        // delete old connections
        $DB->delete($CONFIG->dbprefix.'vms_connections', "end_tstamp < '".time()."'");

    }


    public function serve($request){
        $DB = JFX::registry('db');
        $CONFIG = JFX::registry('config');
        $CRYPT = JFX::registry('JFX_Crypt');
        $USER = JFX::registry('JFX_User');
        $IUSER = JFX::registry('IUsers_User');
        $DATACRYPT = new JFX_Crypt($this->getConfig('crypt-salt'));
        $FCRYPT = new JFX_Crypt('files');

        $request = str_replace('../', '', $request);
        $requests = explode('/', $request);
        foreach($requests as $requestedFile){
            if(trim($requestedFile)=='') continue;
            break;
        }

        // this server is all about json
        while(ob_get_level()>0) ob_end_clean();

        // figure out what they are requesting
        if($this->GetConfig('server-active') == 0 || trim($requestedFile)=='' || !file_exists($this->dirname.'/server/'.$requestedFile.'.php')){
            // no page requested, redirect to the right page
            JFX::redirect(JFX::getUrlById($this->GetConfig('default-public-page')));
        }else{
            if($requestedFile !== 'connect'){
                // check connection
                include($this->dirname.'/server/setup.php');
            }
            include($this->dirname.'/server/'.$requestedFile.'.php');
        }

        // this server is all about json
        exit;

    }




    
    public function admin(){

        $SMARTY = JFX::registry('Smarty');
        $VIEW = JFX::registry('JFX_View');
        $DB = JFX::registry('db');
        $CONFIG = JFX::registry('config');
        $CRYPT = JFX::registry('JFX_Crypt');
        $FCRYPT = new JFX_Crypt('files');

        require_once($this->dirname.'/classes/VersionManager.php');
        require_once($this->dirname.'/classes/FileCompare.php');
        require_once($this->dirname.'/classes/Client.php');
        $VM = new VMS_VersionManager;
        $FC = new VMS_FileCompare;
        $VMC = new VMS_Client;

        JFX::register('VMS_VersionManager', $VM);
        JFX::register('VMS_FileCompare', $FC);

        $SMARTY->assign('vmsUrl', $this->adminUrl);

        $this->addStylesheet('admin.css');

        

        // figure out our request
        $req = explode('?', $_SERVER['REQUEST_URI']);
        $req = explode('/', $req[0]);

        foreach($req as $k=>$v){
            unset($req[$k]);
            if($v == $this->keyname) break;
        }
        
        $req = array_regenerate_keys($req);

        if(count($req)==0) $requestedFile = 'list';
        foreach($req as $k=>$requestedFile) break;

        if(isset($req[$k])) unset($req[$k]);
        $req = array_regenerate_keys($req);

        $requestedFile = str_replace('..', '', $requestedFile);

        $SMARTY->assign('activeTab', $requestedFile);

        echo $this->fetchTemplate('menu.tpl');

        echo '<div id="admin-content">';

        if(file_exists($this->dirname.'/actions/'.$requestedFile.'.php'))
            include($this->dirname.'/actions/'.$requestedFile.'.php');
        else
            include($this->dirname.'/actions/default.php');
 
        echo '</div>';




    }













    function LoadConfig(){
        $DB = JFX::registry('db');
        $CONFIG = JFX::registry('config');
        // make sure we have been installed!

        if(!JFX_Module::isInstalled($this->keyname)) return;

        $config = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}vms_config");

        if($config === false) return;
        foreach($config as $k=>$v){
            $this->VMS_Config[$v['k']] = $v['v'];
        };
    }







    function GetConfig($k){
        if(isset($this->VMS_Config[$k])) return $this->VMS_Config[$k];
        else if($this->ThrowConfigExceptions) throw new Exception($this->getTitle().'->GetConfig was asked to find a non-existing key: '.$k);
        else return false;
    }















    public function install(){
        $VIEW = JFX::registry('JFX_View');
        $SMARTY = JFX::registry('Smarty');
        $USER = JFX::registry('JFX_User');
        $CONFIG = JFX::registry('config');
        $DB = JFX::registry('db');
        $dirname = str_replace(DIRECTORY_SEPARATOR, '/', dirname(__FILE__)).'/install';
        include('install/install.php');
    }


    public function uninstall(){
        $VIEW = JFX::registry('JFX_View');
        $SMARTY = JFX::registry('Smarty');
        $USER = JFX::registry('JFX_User');
        $CONFIG = JFX::registry('config');
        $DB = JFX::registry('db');
        $dirname = str_replace(DIRECTORY_SEPARATOR, '/', dirname(__FILE__)).'/install';
        include('install/uninstall.php');
    }












}